Fix memory leak in shutdown handler using WeakReference#539
Open
JanTvrdik wants to merge 2 commits intoKnpLabs:masterfrom
Open
Fix memory leak in shutdown handler using WeakReference#539JanTvrdik wants to merge 2 commits intoKnpLabs:masterfrom
JanTvrdik wants to merge 2 commits intoKnpLabs:masterfrom
Conversation
The register_shutdown_function was holding a strong reference to $this, preventing garbage collection until script termination. This change wraps the reference in a WeakReference, allowing normal garbage collection while still cleaning up temporary files at shutdown if the object still exists.
There was a problem hiding this comment.
Pull request overview
This PR fixes a memory leak in long-running PHP processes by preventing the shutdown handler from holding a strong reference to generator instances. By using WeakReference, the generator objects can be garbage collected normally while still ensuring temporary files are cleaned up during abnormal script termination.
Changes:
- Replaced direct object reference in shutdown handler with
WeakReferenceto allow garbage collection - Used null-safe operator to safely call
removeTemporaryFiles()only if the object still exists at shutdown
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
register_shutdown_functionholding a strong reference to$thisWeakReferenceto allow normal garbage collection while still cleaning up temporary files at shutdownProblem
The shutdown handler was registered with
[$this, 'removeTemporaryFiles'], which kept a strong reference to the generator object. This prevented garbage collection until script termination, causing memory leaks in long-running processes or when creating many generator instances.Solution
Wrap
$thisin aWeakReferenceso the object can be garbage collected normally. The shutdown handler checks if the object still exists before callingremoveTemporaryFiles().Test plan